<?php

require 'vendor/autoload.php';

use League\HTMLToMarkdown\HtmlConverter;

define('ROOTPATH',dirname(__FILE__).'/storage/');
define('URLPATH','/storage/');

function parse_markdown($article){
	
	$converter = new HtmlConverter();
	$converter->getConfig()->setOption('remove_nodes', 'span time');
	$markdown = $converter->convert($article);
	
	$markdown = preg_replace('/^(\s+)#!(\w+)$/im','',$markdown);
	$markdown = str_replace(['<header>','</section>'],'',$markdown);
	$markdown = preg_replace('/<div class="entry-meta"(.*?)"postContentTrustedHtml">/is','',$markdown);
	
	return $markdown;
}

function parse_title($html){
	$title = 'no_name';
	
	preg_match('/<meta name="description" content="(.*?)">/is',$html,$title_match);
	if(!empty($title_match)){
			$title = $title_match[1];
	}
	
	// windows 上文件名会乱码
	//$title = iconv('utf-8','gb2312//ignore',$title);
	return $title;
}

function get_type($url){
	$type = '';
	$id = 0;
	if (preg_match('%drops\.wooyun\.org\/(\w+)\/(\d+)%', $url, $regs)) {
		$type = 'wooyun_drops';
		$id = $regs[2];
	} 
	
	return [$type,$id];

}

function get_page($url){
	
	list($type,$id) = get_type($url);
	$storage_path = ROOTPATH.$type.'/'.$id.'/';
	
	#文件夹存在，返回文件名
	if(is_dir($storage_path)){
		$handle = opendir($storage_path);
		while(false !== ($file_name = readdir($handle))){
			if(substr($file_name,-3)==='.md'){	
				$file_name = substr($file_name,0,-3);
				break;
			}
		}
	}else{
		# 文件夹不存在，获取文件并返回文件名
		# 获取页面主体
		$html = file_get_contents($url);

		preg_match('/<article([^>]+)>(.*?)<\/article>/is',$html,$article_match);
		preg_match('/entry-content">(.*)entry-ggads"/is', $html,$article_match_old);
		if(!empty($article_match)){
			$article = $article_match[2];
		}elseif(!empty($article_match_old)){
			$article = $article_match[1];
		}else{
			die('no contents');
		}
		
		# 解析页面内容
		$file_name = parse_title($html);
		$markdown = parse_markdown($article);
		
		# 写文件
		mkdir($storage_path,0777,true);
		file_put_contents($storage_path.$file_name.'.md',$markdown);
		
	}
	if(!empty($type) && !empty($file_name)){
		$url = '/'.$type.'/'.$id.'/'.$file_name.'.md';
		$file_name = $file_name.'.md';
		return [ $file_name , $url];
	}else{
		return [ '', ''];
	}
	
}
